require(['monitor'], function () {
	
    $('#monitor').monitor({
        canId: '31',
        callback: function (data) {
        	loadData(data);
        },
        clearData: function () {
        	loadData({});
        }
    });
    
	var _int = 400;
	function fadeIn($li) {
		if ($li.next().length) {
			$li.next().fadeIn(_int, function() {
				fadeIn($li.next())
			})
		} else {
			var $parent = $li.parent();
			var $parentNext = $parent.prev();
			if ($parentNext.length) {
				$parentNext.slideDown(400, function() {
					$('li:eq(0)', $(this)).fadeIn(_int, function() {
						$(this).next().fadeIn(100, fadeIn($(this)));
					});
				})
			} else {
				$(".monitor-panel2 .text-box").addClass("active");
			}
		}
	}
    
	function animation() {
		$(".monitor-panel2 ul:last li:eq(0)").fadeIn(_int, function() {
			$(this).next().fadeIn(100, fadeIn($(this)));
		});
	}
    
	//解析
	function loadData(data) {
		if (!$.isEmptyObject(data)) {
			$("#tirePressureClass").removeClass("monitor-panel2-gray")
			$(".temp-div").hide();
			animation();
			//轮胎个数
			var tireCount = data.tireCount;
			//总里程
			var totalMile = data.totalMile;
			//轮轴位置
			var axleLoc = data.axleLoc.split("/");
			//轮胎左右位置
			var tireLeftRight = data.tireLeftRight.split("/");
			var tireLeftRightText = "";
			//轮胎内外位置
			var tireInOut = data.tireInOut.split("/");
			var tireInOutText = "";
			//传感器
			var sensorID = data.sensorID.split("/");
			//信号情况
			var signSt = data.signSt.split("/");
			var signStText = "";
			//轮胎压力
			var tirePress = data.tirePress.split("/");
			//轮胎温度
			var tempt = data.tempt.split("/");
			//告警情况（第一位轮胎高温报警情况(0:正常 1 高温报警)，第二位压力检测(0 超高压报警压力1 高压报警2 正常压力3 低压报警4 超低压报警)）
			var alarmSt = data.alarmSt.split("/");
			//轮胎高温报警情况
			var alarmStOne = "";
			//压力检测
			var alarmStTwo = "";
			//轮胎位置
			var tireText = "";
			//HTML
			var detailHtml = "";
			//经纬度
			$("#hideLoLgt").val(data.loLgt);
			$("#hideLoLtt").val(data.loLtt);
	
			//车速
			$("#speedOfAMotorVehicle").html(data.speedOfAMotorVehicle + "km/h");
			//里程
			$("#totalMile").html(data.totalMile + "km");
			//车辆位置
			$("#preNextStnName").html(data.preNextStnName);
			$("#detailHtml").empty();
	
			for ( var i = 0; i < sensorID.length - 1; i++) {
				//轮胎左右位置
				if (tireLeftRight[i] == 0) {
					tireLeftRightText = "左";
				} else if (tireLeftRight[i] == 1) {
					tireLeftRightText = "右";
				}
				//轮胎内外位置
				if (tireInOut[i] == 0) {
					tireInOutText = "内";
				} else if (tireInOut[i] == 1) {
					tireInOutText = "外";
				}
				//信号情况
				if (signSt[i] == 0) {
					signStClass = "";
					signStText = "正常";
				} else if (signSt[i] == 1) {
					signStClass = "warning";
					signStText = "未收到轮胎信号";
				}
				else {
					signStClass = "warning";
					signStText = signSt[i];
				}
				//轮胎高温
				alarmStOne = alarmSt[i].substring(0, 1);
				var alarmStOneClass = "";
				if (alarmStOne == 0) {
					alarmStOneClass = "";
					alarmStOneText = "正常";
				} else if (alarmStOne == 1) {
					alarmStOneClass = "warning";
					alarmStOneText = "高温报警";
				}
				else {
					alarmStOneClass = "warning";
					alarmStOneText = alarmStOne;
				}
				//压力检测
				alarmStTwo = alarmSt[i].substring(1, 2);
				var alarmStTwoClass = "";
				if (alarmStTwo == 0) {
					alarmStTwoClass = "warning";
					alarmStTwoText = "超高压报警压力";
				} else if (alarmStTwo == 1) {
					alarmStTwoClass = "warning";
					alarmStTwoText = "高压报警";
				} else if (alarmStTwo == 2) {
					alarmStTwoClass = "";
					alarmStTwoText = "正常压力";
				} else if (alarmStTwo == 3) {
					alarmStTwoClass = "warning";
					alarmStTwoText = "低压报警";
				} else if (alarmStTwo == 4) {
					alarmStTwoClass = "warning";
					alarmStTwoText = "超低压报警";
				}
				else{
					alarmStTwoClass = "warning";
					alarmStTwoText = alarmStTwo;
				}
				//用来区分不同地方的胎压 0-康驰胎压 1-福州
				var symbol = data.symbol
				if (symbol == 1) {
					tireText = "第" + (i + 1) + "个轮胎"
				} else {
					tireText = axleLoc[i] + "轴" + tireLeftRightText + "侧"
							+ tireInOutText + "轮胎"
				}
				var val = {
					val1 : tireText,
					val2 : sensorID[i],
					class1 : signStClass,
					val3 : signStText,
					val4 : tirePress[i],
					val5 : tempt[i],
					class2 : alarmStOneClass,
					val6 : alarmStOneText,
					class3 : alarmStTwoClass,
					val7 : alarmStTwoText
				}
				var srcs = [ "../../content/images/icon5.png",
						"../../content/images/icon6.png",
						"../../content/images/icon7.png",
						"../../content/images/icon8.png",
						"../../content/images/icon9.png",
						"../../content/images/icon11.png",
						"../../content/images/icon10.png" ];
				var styles = [ "left:32px;top:31px;", "left:36px;top:36px;",
						"left:29px;top:35px;", "left:28px;top:30px;",
						"left:29px;top:30px;", "left:34px;top:34px;",
						"left:32px;top:33px;" ];
				var tips = [ "轮胎位置", "轮胎传感器ID", "轮胎信号", "轮胎压力", "轮胎温度", "轮胎温度报警",
						"轮胎压监测" ];
				var classes = [ "item", "item", "item {class1}", "item", "item",
						"item {class2}", "item {class3}", ];
				var xx = '<ul class="item-list clearfix"style="display:block;">';
				for ( var j = 0; j < 7; j++) {
					xx += '	<li class="' + classes[j] + '">'
							+ '      <div class="item-inner">'
							+ '          <div class="item-bg"></div>'
							+ '         	<img src="' + srcs[j] + '" style="'
							+ styles[j] + '">' + '    	</div>'
							+ '   	<div class="text-box">{val' + (j + 1)
							+ '}</div>' + '  	<h4>' + tips[j] + '</h4>' + '	</li>';
				}
				xx += '</ul>';
				$("#detailHtml").append(xx.format(val));
			}
		}
	}
	
});